package ils.di;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author: Rejeev Divakaran
 * Date: 7/23/13
 */
public class ObjectBuilderFactory {
    private static final Logger LOGGER = LoggerFactory.getLogger(ObjectBuilderFactory.class);
    private static String objectBuilderClassName = ObjectBuilderImpl.class.getName();
    private static ObjectBuilder objectBuilder;

    public static synchronized ObjectBuilder get(){
        if(objectBuilder != null) return objectBuilder;
        else {
            try {
                Class klass = Class.forName(objectBuilderClassName);
                objectBuilder = (ObjectBuilder)klass.newInstance();
                return objectBuilder;
            } catch (Exception e) {
                LOGGER.error("Execption while creating ObjectBuilder", e);
                throw new RuntimeException("Execption while creating ObjectBuilder");
            }
        }
    }

    public static void setObjectBuilderClassName(String objectBuilderClassName) {
        ObjectBuilderFactory.objectBuilderClassName = objectBuilderClassName;
        objectBuilder = null;
    }

    static {
        String className = System.getProperty("objectBuilder");
        if(className != null) ObjectBuilderFactory.objectBuilderClassName = className;
    }
}
